package com.soup.view.component;

import com.soup.view.value.ColorValue;
import com.soup.view.value.SizeValue;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Context extends JPanel{
    private static final long serialVersionUID = 1L;
    public Context(int width,int height) {
        this.setBounds(0, 25, width, height- SizeValue.TopNavigationHeight);
        this.setBackground(ColorValue.ContextBackground);
        this.setLayout(null);
        this.setForeground(ColorValue.ContextForeground);
        this.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, ColorValue.ContextBackground));
    }

    public void add(JTextComponent component, String name, int margin) {
        component.setFont(new Font("黑体", Font.BOLD, SizeValue.ContextFont));
        component.setLayout(null);
        JLabel lael = new JLabel(name);
        lael.setFont(new Font("幼圆", Font.BOLD, SizeValue.ContextFont));
        lael.setBounds(0, 0, margin, component.getHeight());
        lael.setOpaque(true);
        lael.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, ColorValue.Border));
        component.add(lael);
        lael.setBackground(ColorValue.ContextBackground);
        component.setBackground(ColorValue.ContextBackground);
        component.setForeground(Color.GRAY);
        lael.setForeground(ColorValue.ContextForeground);
        component.setBorder(BorderFactory.createMatteBorder(1, margin, 1, 1, ColorValue.Border));
        component.setCaretColor(ColorValue.ContextForeground);
        component.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                component.setText("");
                component.setForeground(ColorValue.ContextForeground);
            }
        });

        component.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {

            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                component.setForeground(ColorValue.ContextForeground);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });
        super.add(component);
    }

    public void add(JLabel temp,int font_size) {
        temp.setBackground(ColorValue.ContextBackground);
        temp.setForeground(ColorValue.ContextForeground);
        temp.setFont(new Font("幼圆", Font.BOLD, font_size));
        super.add(temp);
    }

    public void add(JButton temp,int font_size) {
        temp.setBackground(ColorValue.ContextBackground);
        temp.setForeground(ColorValue.ContextForeground);
        temp.setFont(new Font("幼圆", Font.BOLD, font_size));
        temp.setFocusable(false);
        temp.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, ColorValue.Border));
        super.add(temp);
    }

    public void add(JRadioButton temp,int font_size) {
        temp.setBackground(ColorValue.ContextBackground);
        temp.setForeground(ColorValue.ContextForeground);
        temp.setFont(new Font("幼圆", Font.BOLD, font_size));
        temp.setFocusable(false);
        super.add(temp);
    }
}